package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelRuleDetailEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelRuleEntity;
import com.dragon.modules.yuegou.enums.code.MarketingCode;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleIsDefault;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelRuleStatus;
import com.dragon.modules.yuegou.enums.merchant.MerchantLevelStatus;
import com.dragon.modules.yuegou.mapper.merchant.MerchantLevelRuleMapper;
import com.dragon.modules.yuegou.model.merchant.MerchantLevelRule;
import com.dragon.modules.yuegou.model.merchant.MerchantLevelRuleDetail;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelRuleDetailQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelRuleQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelRuleDetailService;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelRuleService;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 等级规则 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 15:26:31
 * @version 2.0
 */
@Service("merMerchantLevelRuleService")
public class MerchantLevelRuleServiceImpl
		extends ServiceImpl<MerchantLevelRuleMapper, MerchantLevelRuleEntity>
		implements MerchantLevelRuleService {
	
	@Autowired
	private MerchantLevelRuleDetailService merchantLevelRuleDetailService;

	@Autowired
	private MerchantLevelService merchantLevelService;
	
	@Override
	public MerchantLevelRule getLevelRule(String id) {

		MerchantLevelRule merchantLevelRule = new MerchantLevelRule(getById(id));

		MerchantLevelRuleDetailQuery merchantLevelRuleDetailQuery = new MerchantLevelRuleDetailQuery();
		merchantLevelRuleDetailQuery.setLevelRuleId(id);
		List<MerchantLevelRuleDetailEntity> list = merchantLevelRuleDetailService.list(merchantLevelRuleDetailQuery.toQueryWrapper());
		merchantLevelRule.setLevelRuleDetailList(list.stream().map(MerchantLevelRuleDetail::new).collect(Collectors.toList()));
		return merchantLevelRule;
	}

	@Override
	public void shelveOrDown( String id) {
		Check.notNull(id, MarketingCode.ID_NOTNULL.getMessage());
		//判断是否是基础商家，是基础商家无下架权限
		MerchantLevelRule merchantLevelRule = new MerchantLevelRule(getById(id));
		if (Objects.equals(merchantLevelRule.getLevelName(),"基础商家"))
			return;
		if (MerchantLevelRuleStatus.NORMAL.equals(merchantLevelRule.getStatus())) {
			merchantLevelRule.setStatus(MerchantLevelRuleStatus.DISABLE);
		} else {
			merchantLevelRule.setStatus(MerchantLevelRuleStatus.NORMAL);
		}
		updateById(merchantLevelRule);
	}

	@Override
	@Transactional
	public void insertMerchantLevelRule(MerchantLevelRule query) {
		Check.notEmpty(query.getLevelName(),"等级名称不能为空");
		Check.notNull(query.getStatus(),"等级规则状态不能为空");
		Check.notEmpty(query.getImg(),"等级图片不能为空");
		save(query);

		List<MerchantLevelRuleDetail> levelRuleDetailList = query.getLevelRuleDetailList();
		if (Objects.nonNull(levelRuleDetailList)) {
			ArrayList<MerchantLevelRuleDetailEntity> merchantLevelRuleDetails = new ArrayList<>();
			for (MerchantLevelRuleDetail merchantLevelRuleDetail : levelRuleDetailList) {
				if (Objects.nonNull(merchantLevelRuleDetail)) {
					merchantLevelRuleDetail.setLevelRuleId(query.getId());
					merchantLevelRuleDetails.add(merchantLevelRuleDetail);
				}
			}
			merchantLevelRuleDetailService.saveBatch(merchantLevelRuleDetails);
		}
	}

	/**
	 * 按等级名称模糊查询
	 * @param page
	 * @param merchantLevelRule
	 * @return
	 */
	@Override
	public IPage<MerchantLevelRule> searchList(PageRequest page, MerchantLevelRule merchantLevelRule) {
		MerchantLevelRuleQuery query = new MerchantLevelRuleQuery();
		if (null != merchantLevelRule.getLevelName()) {
			query.levelName().like(merchantLevelRule.getLevelName());
		}
		QueryWrapper<MerchantLevelRuleEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByDesc(MerchantLevelRuleEntity.COL_CREATED_TIME);
		return page(PageToResultUtils.resultToPage(page), wrapper);
	}

	@Override
	public List<MerchantLevelRule> searchLevelName() {
		MerchantLevelRuleQuery query = new MerchantLevelRuleQuery();
		query.setStatus(MerchantLevelRuleStatus.NORMAL);
		List<MerchantLevelRuleEntity> list = list(query.toQueryWrapper());
		return list.stream().map(MerchantLevelRule::new).collect(Collectors.toList());
	}

	/**
	 * 修改商家等级规则
	 * @param query
	 */
	@Override
	@Transactional
	public void updateMerchantLevelRule(MerchantLevelRule query) {
		Check.notEmpty(query.getLevelName(),"等级名称不能为空");
		Check.notNull(query.getStatus(),"等级规则状态不能为空");
		Check.notEmpty(query.getImg(),"等级图片不能为空");
		updateById(query);
		List<MerchantLevelRuleDetail> levelRuleDetailList1 = query.getLevelRuleDetailList();
		MerchantLevelRuleDetailQuery merchantLevelRuleDetailQuery = new MerchantLevelRuleDetailQuery();
		merchantLevelRuleDetailQuery.setLevelRuleId(query.getId());
		List<MerchantLevelRuleDetailEntity> list = merchantLevelRuleDetailService.list(merchantLevelRuleDetailQuery.toQueryWrapper());
		List<String> collect = list.stream().map(MerchantLevelRuleDetailEntity::getId).collect(Collectors.toList());
		ArrayList<String> strings = new ArrayList<>();

		for (MerchantLevelRuleDetail merchantLevelRuleDetail : levelRuleDetailList1) {
			if (Objects.isNull(merchantLevelRuleDetail.getId())) {
				merchantLevelRuleDetail.setLevelRuleId(query.getId());
				merchantLevelRuleDetailService.save(merchantLevelRuleDetail);
			} else {
				merchantLevelRuleDetailService.updateById(merchantLevelRuleDetail);
				strings.add(merchantLevelRuleDetail.getId());
			}
		}
		for (String s : collect) {
			if (!strings.contains(s)) {
				merchantLevelRuleDetailService.removeById(s);
			}
		}
	}

	@Override
	public List<MerchantLevelRule> getDetailList() {
		MerchantLevelRuleQuery merchantLevelRuleQuery = new MerchantLevelRuleQuery();
		merchantLevelRuleQuery.setStatus(MerchantLevelRuleStatus.NORMAL);
		List<MerchantLevelRule> list = list(merchantLevelRuleQuery.toQueryWrapper()).stream().map(MerchantLevelRule::new).collect(Collectors.toList());
		for (MerchantLevelRule merchantLevelRule : list) {
			//详情通过等级规则id 和 结束时间大于等于当前时间
			MerchantLevelRuleDetailQuery merchantLevelRuleDetailQuery = new MerchantLevelRuleDetailQuery();
			merchantLevelRuleDetailQuery.setLevelRuleId(merchantLevelRule.getId());
			List<MerchantLevelRuleDetailEntity> list1 = merchantLevelRuleDetailService.list(merchantLevelRuleDetailQuery.toQueryWrapper());

			merchantLevelRule.setLevelRuleDetailList(list1.stream().map(MerchantLevelRuleDetail::new).collect(Collectors.toList()));

		}
		return list;
	}

	@Override
	public List<MerchantLevelRule> getBuyList() {
		List<MerchantLevelRule> detailList = getDetailList();
		ArrayList<MerchantLevelRule> list = new ArrayList<>();
		for (MerchantLevelRule merchantLevelRule : detailList) {
			if (!MerchantLevelRuleStatus.NORMAL.equals(merchantLevelRule.getStatus())) {
				list.add(merchantLevelRule);
			}
		}
		return list;
	}

	@Override
	public List<MerchantLevelRule> getList(String merchantId) {
		//查询商户当前等级
		MerchantLevelQuery query = new MerchantLevelQuery();
		query.setMerchantId(merchantId);
		query.setStatus(MerchantLevelStatus.PAID);
		
		MerchantLevelEntity merchantLevel = merchantLevelService.getOne(query.toQueryWrapper());
		
		//查询所有等级信息列表
		MerchantLevelRuleQuery levelRuleQuery = new MerchantLevelRuleQuery();
		levelRuleQuery.setStatus(MerchantLevelRuleStatus.NORMAL);
		List<MerchantLevelRule> list = list(levelRuleQuery.toQueryWrapper()).stream().map(MerchantLevelRule::new).collect(Collectors.toList());
		
		if (merchantLevel != null && StringUtils.isNotEmpty(merchantLevel.getLevelId())) {
			list.forEach(levelRule -> levelRule.setWhether(merchantLevel != null && levelRule.getId().equals(merchantLevel.getLevelId())));
		}else {
			list.forEach(levelRule ->levelRule.setWhether(levelRule.getIsDefault().equals(MerchantLevelRuleIsDefault.YES) && levelRule.getStatus().equals(MerchantLevelRuleStatus.NORMAL)));
		}

		return list;
	}
}